package org.sixtynine.stock.dao.impl;

/**
 * @author Thilan
 *
 */

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.sixtynine.stock.Constants;
import org.sixtynine.stock.dao.PackageDataDao;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.PackageData;
import org.springframework.stereotype.Repository;

@Repository
public class PackageDataDaoImpl extends AbstractDaoImpl<PackageData, Integer>
		implements PackageDataDao {

	protected PackageDataDaoImpl() {
		super(PackageData.class);
	}

	@Override
	public void savePackageData(PackageData packageData) {
		saveOrUpdate(packageData);
	}

	@Override
	public List<PackageData> findPackageData(int duration) {
		Criteria criteria = getCurrentSession().createCriteria(
				PackageData.class);
		return findByCriteria(criteria.add(Restrictions
				.eq("duration", duration)));
	}

	@Override
	public PackageData findPackageData(String name) {
		Criteria criteria = getCurrentSession().createCriteria(
				PackageData.class);
		if (findByCriteria(criteria.add(Restrictions.eq("name", name))).size() > 0) {
			return findByCriteria(criteria.add(Restrictions.eq("name", name)))
					.get(0);
		} else {
			return null;
		}

	}

	@Override
	public Page<PackageData> searchPackageData(int duration, String name,
			String orderBy, String orderByName, GridPramDTO gridPramDTO) {
		Criteria criteria = getCurrentSession().createCriteria(
				PackageData.class);

		// criteria.addOrder(Order.desc(getRecordOrder(orderBy)));

		// criteria.addOrder(Order.asc(getRecordOrder(orderByName)));

		if (duration > Constants.ERROR) {
			criteria.add(Restrictions.eq("duration", duration));
		}
		if (name != null) {
			criteria.add(Restrictions.ge("name", name));
		}
		return findByCriteria(criteria, gridPramDTO);
	}

}
